The knowledge base (KB) defines the ΓÇ£knowledgeΓÇ¥ of a rule based system, whether static or dynamic. This knowledge is in the form of production rules and facts. The extent of the knowledge in a system is called its domain.
The rule base comprises the static knowledge of a rule based system. This knowledge may be in the form of rules or facts and is input by the knowledge engineer. This is the "code" of a rule based system.
The dynamic part of the knowledge base is sometimes known as "working memory". It includes those facts established at run time and the state of the inference process. The process for establishing these facts and the operation of the inference process will become clear in the tutorial.
Rules define conditions which, when met, lead to establishing facts or drawing conclusions. They generally appear as "if-then" statements. If the conditions in the body of the rule are met, then the assertions or conclusions in the "then" part of the rule are made. The individual conditions within the body of a rule are called ΓÇ£clausesΓÇ¥. The rule "Corvette" below illustrates these components. If the rule "Corvette" below proves true, it will assert the fact "model".
rule Corvette -- rule ID
if
fact make = Chevy and -- clause
fact passengers = 2 -- clause
then
put Corvette into fact model -- conclusion (assert
-- fact "model")
Facts are ΓÇ£truthsΓÇ¥ or ΓÇ£knownsΓÇ¥ that are established or ΓÇ£assertedΓÇ¥ by the system, whether static or dynamic. Facts may be asserted by a user's response to a question (dynamic), by the outcome of a rule (dynamic), or in the rule base
(static).
In the following paragraphs, we will build a rule base for a simple rule based system. We will use this rule base for the rest of the tutorial.
LetΓÇÖs say that we would like to establish a rule base for determining whether a vehicle on a lot was a Corvette, a Camaro, or a Mustang. First of all, we must establish some rules that can differentiate among the three cars. The make of the car can differentiate between a Mustang and a Corvette or a Camaro. The number of passengers can differentiate between a Corvette and a Camaro. The rules indicated below provide a start for our system.
rule Corvette -- rule ID
if
fact make = Chevy and -- clause
fact passengers = 2 -- clause
then
put Corvette into fact model -- conclusion (assert
-- fact "model")
rule Camaro
if
fact make = Chevy and
fact passengers = 4
then
put Camaro into fact model
rule Mustang
if
fact make = Ford
then
put Mustang into fact model
These rules look fine for distinguishing among the three models, but let's say that the person using the system doesn't know a car from a truck and the lot contains trucks and motorcycles in addition to the three models of cars. In that case, we must establish rules that can identify the vehicle as a car. The rules below accomplish this, so we will add them the rule base.
rule "car 1"
if
fact wheels = 4 and
fact passengers <= 4 and
fact "used for cargo" = no
then
put true into fact car
rule "car 2"
if
not (fact wheels = 4)
then
put false into fact car
rule "car 3"
if
not (fact passengers <= 4)
then
put false into fact car
rule "car 4"
if
not (fact "used for cargo" = no)
then
put false into fact car
NOTE: We have to include rules containing the "not" of each clause in the rule
"car 1" so that, during the inference process, the fact "car" is sure to be asserted either true or false. The purists say that you shouldn't use ELSE or OR in a rule based system. Otherwise, we could simply add the statement
"else put false into car" to rule "car 1". Generally, in rule based systems, OR is accomplished by generating individual rules to establish each case of the OR. In our example, rules "car 2", "car 3", and "car 4" represent the cases of an OR statement that can result in fact "car" being asserted "false". Thus, these three rules are equivalent to the following statement:
if
not (fact wheels = 4) or
not (fact passengers <= 4) or
not (fact "used for cargo" = no) or
then
put false into fact car
The reasons for making certain that facts get asserted will become clear later when we address the inference engine.
Now that we have rules to determine the fact "car", we need to incorporate this fact into the rules determining the car's model. We do this by adding the clause "fact car" to each of the rules establishing "model" as seen below.
rule Corvette
if
fact car and
fact make = Chevy and
fact passengers = 2
then
put Corvette into fact model
rule Camaro
if
fact car and
fact make = Chevy and
fact passengers = 4
then
put Camaro into fact model
rule Mustang
if
fact car and
fact make = Ford
then
put Mustang into fact model
The inference engine will use the rules "car 1", "car 2", "car 3", and "car 4" to assert fact "car" which will in turn be used to assert fact "model" through rules "Corvette", "Camaro", and "Mustang".
The seven rules above represent a simple rule base. You will note that our
"knowledge" is severely limited. Our domain in this case includes car models Corvette, Camaro, and Mustang. Our rather ignorant "expert" system "knows" about nothing else (in fact, it only knows how to distinguish between the three models). You will also notice that there is no control apparent in our rule base. As you will see, the inference engine will determine the control.
Now that we have a sample rule base, we need a mechanism to tell the inference engine how and when to communicate with the user. We will delve into the user interface only far enough to establish this mechanism.
First, we need a way to query the user about facts. The facts that the user will have to specify include "make", "passengers", "wheels", and "used for
"cargo". We don't need to ask about "model" or "car" because we have rules that will assert them.
We will use the keyword "FACT" to indicate a list of facts and their appropriate queries. The inference engine will use that information to decide what prompt to display to the user when asking about a certain fact. We need to add the fact declarations and their queries to the front of our rule base as shown below.
fact
passengers
"How many passengers can it carry?",
wheels
"How many wheels does it have?",
"used for cargo"
"Is it used to carry cargo?",
make
"What is the make of the vehicle?"
We also need a way to inform the user that the system has reached a conclusion for rules "Corvette", "Camaro", and "Mustang" since they determine the fact
"model" (fact "model" is our "goal", or what we are trying to determine). We will use the keyword "CONCLUDE" to do this. So, we modify the three rules to reflect this change.
rule Corvette
if
fact car and
fact make = Chevy and
fact passengers = 2
then
put Corvette into fact model
conclude "The car is a Corvette."
rule Camaro
if
fact car and
fact make = Chevy and
fact passengers = 4
then
put Camaro into fact model
conclude "The car is a Camaro."
rule Mustang
if
fact car and
fact make = Ford
then
put Mustang into fact model
conclude "The car is a Mustang."
We will be using this sample rule base throughout the rest of the tutorial. The complete rule base appears at the end of this tutorial.
Now that we have this "knowledge" established, we need an inference engine to use it. The next section begins an examination of the inference engine.